package view.SkillBar
{
	import starling.core.starling_internal;
	import starling.display.Stage;
	import starling.events.EventDispatcher;
	import starling.events.KeyboardEvent;
	
	import utils.LayerUtils;
	import utils.LevelConfigXmlUtils;
	
	import view.Bullet.BulletForTipManager;
	import view.Bullet.BulletForTipView;
	import view.FireBulletVO;
	import view.Prize.PrizeView;
	
	public class SkillBarManager extends EventDispatcher
	{
		private static var _instance:SkillBarManager;
		private var _skillBar:SkillBarView;
		public function SkillBarManager()
		{
			super();
		}
		
		public static function getInstance():SkillBarManager{
			return _instance ||= new SkillBarManager();
		}
		
		public function start(stage:Stage):void{
			FireBulletVO.fireState = FireBulletVO.SYSTEM_STATE;
			_skillBar = new SkillBarView();
			LayerUtils.getInstance().frameLayer.addChild(_skillBar);
			_skillBar.x = Const.SKILL_BAR_X;
			_skillBar.y = Const.SKILL_BAR_Y;
			
			stage.addEventListener(KeyboardEvent.KEY_DOWN, onKeyDownHandler);
		}
		
		private function onKeyDownHandler(e:KeyboardEvent):void
		{
			switch(e.keyCode){
				case 49:   //键盘1号键
					if(_skillBar.bulletBoxView1.isEmpty)
						return;
					disShowSelectedAni();
					_skillBar.bulletBoxView1.showSelectdAni();
					FireBulletVO.fireState = FireBulletVO.SKILLBAR_STATE;
					BulletForTipManager.getInstance().tipId = _skillBar.bulletBoxView1.bulletNo;
					BulletForTipManager.getInstance().showNextBulletTip(BulletForTipManager.getInstance().tipId);
					break;
				case 50:   //键盘2号键
					if(_skillBar.bulletBoxView2.isEmpty)
						return;
					disShowSelectedAni();
					_skillBar.bulletBoxView2.showSelectdAni();
					FireBulletVO.fireState = FireBulletVO.SKILLBAR_STATE;
					BulletForTipManager.getInstance().tipId = _skillBar.bulletBoxView2.bulletNo;
					BulletForTipManager.getInstance().showNextBulletTip(BulletForTipManager.getInstance().tipId);
					break;
				case 51:   //键盘3号键
					if(_skillBar.bulletBoxView3.isEmpty)
						return;
					disShowSelectedAni();
					_skillBar.bulletBoxView3.showSelectdAni();
					FireBulletVO.fireState = FireBulletVO.SKILLBAR_STATE;
					BulletForTipManager.getInstance().tipId = _skillBar.bulletBoxView3.bulletNo;
					BulletForTipManager.getInstance().showNextBulletTip(BulletForTipManager.getInstance().tipId);
					break;
				case 32:   //键盘空格键
					if(FireBulletVO.fireState == FireBulletVO.SYSTEM_STATE)
						return;
					disShowSelectedAni();
					FireBulletVO.fireState = FireBulletVO.SYSTEM_STATE;
					BulletForTipManager.getInstance().showNextBulletTip(FireBulletVO.randomId());
					break;
			}
		}
		
		private function disShowSelectedAni():void{
			for(var i:uint = 0;i< 3;i++){
				_skillBar.boxVec[i].disShowSelectedAni();
			}
		}
		//当飞行的特殊子弹被打下来后，道具栏里对应的数量加1
		public function addBullet(prizeView:PrizeView):void{
			for(var i:uint = 0;i < 3; i++){
				if(_skillBar.boxVec[i].prizeNo == prizeView.prizeNo){
					_skillBar.boxVec[i].num ++;
					break;
				}
				if(_skillBar.boxVec[i].isEmpty){
					_skillBar.boxVec[i].specialBulletImg = prizeView.prizeImg;
				    //_skillBar.boxVec[i].specialBulletImg =
                    //prizeView.clonePrzieImg();;
					_skillBar.boxVec[i].prizeNo = prizeView.prizeNo;
					_skillBar.boxVec[i].num ++;
					break;
				}else{
					continue;
				}
			}
		}
		
		//当打出去某种特殊子弹的时候，数量减1
		public function subBullet(id:uint):void{
			for(var i:uint = 0;i < 3;i++){
				if(_skillBar.boxVec[i].bulletNo == id){
					_skillBar.boxVec[i].num--;
					if(_skillBar.boxVec[i].num == 0){
						disShowSelectedAni();
						FireBulletVO.fireState = FireBulletVO.SYSTEM_STATE;
					}
					break;
				}
			}
		}
		
		
/*--------------------------------getter and setter----------------------------*/
		public function get skillBar():SkillBarView
		{
			return _skillBar;
		}
	}
}
